package com.bzkj.web.controller.ncc;

import com.bzkj.basicinfo.domain.ncc.NccBasicCustomer;
import com.bzkj.basicinfo.domain.ncc.dto.NccBasicCustomerDto;
import com.bzkj.basicinfo.domain.ncc.vo.CustomerExportVo;
import com.bzkj.basicinfo.service.ncc.NccBasicCustomerService;
import com.bzkj.common.annotation.LogC;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.poi.CsvExportUtils;
import com.bzkj.common.utils.poi.ExcelPDFUtil;
import com.bzkj.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "客户管理")
@RestController
@RequestMapping("/ncc/basic/Customer")
public class NccBasicCustomerController extends BaseController
{

    @Autowired
    private NccBasicCustomerService customerService;

    /**
     * NCC客户基础信息定时任务
     */
    @GetMapping({"/nccBasicCustomerJob", "/nccBasicCustomerJob/{all}"})
    @PreAuthorize("@ss.hasPermi('ncc:Customer:Job')")
    public void nccBasicCustomerJob(@PathVariable(required = false) String all)
    {
        customerService.updateNccBasicCustomerJob(all);
    }


    /**
     * ncc客户基础信息新增
     *
     * @Author wangzhichao
     * @Date 2024/12/05 08:33:57
     * @Param entity 客户信息
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:add')")
    @PostMapping("/nccBasicCustomerSave")
    @ApiOperation(value = "新增接口", httpMethod = "POST")
    @LogC(title = "ncc客户信息", businessType = BusinessType.INSERT,
            tableNames = "ncc_basic_customer", pkFieldNames = "id", content = "' 客户名称：'+#entity.name")
    public AjaxResult nccBasicCustomerSave(@Validated @RequestBody NccBasicCustomer entity)
    {
        return customerService.nccBasicCustomerSave(entity);
    }


    /**
     * ncc客户基础信息状态修改
     *
     * @Author wangzhichao
     * @Date 2024/12/05 09:52:46
     * @Param status
     * @Param pkCustomer
     */
    @ApiOperation("修改状态接口")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:stateEdit')")
    @PutMapping("/nccBasicCustomerEnableStatusUpdate")
    @LogC(title = "ncc客户信息", businessType = BusinessType.UPDATE,
            tableNames = "ncc_basic_customer", pkFieldNames = "pk_customer", content = "' 客户ID：'+#pkCustomer")
    public AjaxResult nccBasicCustomerEnableStatusUpdate(@RequestParam(value = "status", defaultValue = "1") String status, @RequestParam(value = "pkCustomer") String pkCustomer)
    {
        return customerService.nccBasicCustomerEnableStatusUpdate(status, pkCustomer);
    }

    /**
     * 根据pK_CUSTOMER修改客户信息
     *
     * @Author wangzhichao
     * @Date 2024/12/05 14:21:46
     * @Param entity
     */
    @ApiOperation("修改接口")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:edit')")
    @PutMapping("/nccBasicCustomerUpdate")
    @LogC(title = "ncc客户信息", businessType = BusinessType.UPDATE,
            tableNames = "ncc_basic_customer", pkFieldNames = "pk_customer", content = "' 客户名称：'+#entity.name")
    public AjaxResult nccBasicCustomerUpdate(@RequestBody NccBasicCustomer entity)
    {
        return customerService.nccBasicCustomerUpdate(entity);
    }

    /**
     * 根据条件分页查询数据
     *
     * @Author wangzhichao
     * @Date 2024/12/05 15:27:03
     * @Param dto 条件
     * @Param page 分页条件
     */
    @ApiOperation("查询接口")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:query')")
    @GetMapping("/findNccBasicCustomerDataPage")
    public TableDataInfo findNccBasicCustomerDataPage(NccBasicCustomerDto dto)
    {
        startPage();
        return getDataTable(customerService.findNccBasicCustomerDataPage(dto));
    }

    /**
     * 模糊查询客户名称
     *
     * @param name
     * @return
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:query')")
    @GetMapping("/findNccBasicCustomerName")
    public List<String> findNccBasicCustomerName(String name)
    {
        return customerService.findNccBasicCustomerName(name);
    }


    /**
     * 定时任务
     *
     * @return
     */
    @GetMapping("jobMethod")
    public R jobMethod()
    {
        return customerService.jobMethodExecute(false);
    }


    @ApiOperation("导出excel")
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:export')")
    @PostMapping("customerExport")
    @LogC(title = "ncc客户信息", businessType = BusinessType.EXPORT)
    public void customerExport(NccBasicCustomerDto dto, HttpServletResponse response)
    {
        List<NccBasicCustomer> list = customerService.findNccBasicCustomerDataPage(dto);
        List<CustomerExportVo> collect = list.parallelStream().map(item -> {
            CustomerExportVo temp = new CustomerExportVo();
            BeanUtils.copyProperties(item, temp);
            return temp;
        }).collect(Collectors.toList());
        ExcelUtil<CustomerExportVo> util = new ExcelUtil<>(CustomerExportVo.class);
        util.exportExcel(response, collect, "客户信息", "客户信息");
    }

    /**
     * 导出pdf
     * @param dto
     * @param response
     * @return
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:export')")
    @PostMapping("customerExportPdf")
    @LogC(title = "ncc客户信息", businessType = BusinessType.EXPORTPDF)
    public AjaxResult customerExportPdf(NccBasicCustomerDto dto, HttpServletResponse response)
    {
        List<NccBasicCustomer> list = customerService.findNccBasicCustomerDataPage(dto);
        List<CustomerExportVo> collect = list.parallelStream().map(item -> {
            CustomerExportVo temp = new CustomerExportVo();
            BeanUtils.copyProperties(item, temp);
            return temp;
        }).collect(Collectors.toList());
        ExcelPDFUtil<CustomerExportVo> util = new ExcelPDFUtil<>(CustomerExportVo.class);
        util.exportPDF(response, collect, "客户信息", "客户信息", getUsername(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), "全量");
        return AjaxResult.success();
    }

    /**
     * 导出csv
     * @param dto
     * @param response
     * @return
     */
    @PreAuthorize("@ss.hasPermi('basic:nccBasic:export')")
    @PostMapping("customerExportCsv")
    @LogC(title = "ncc客户信息", businessType = BusinessType.EXPORTCSV)
    public AjaxResult customerExportCsv(NccBasicCustomerDto dto, HttpServletResponse response) {
        List<NccBasicCustomer> list = customerService.findNccBasicCustomerDataPage(dto);
        List<CustomerExportVo> collect = list.parallelStream().map(item -> {
            CustomerExportVo temp = new CustomerExportVo();
            BeanUtils.copyProperties(item, temp);
            return temp;
        }).collect(Collectors.toList());
        try {
            CsvExportUtils.exportToWeb(response, "客户信息.csv", collect);
            return AjaxResult.success("导出成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("导出失败");
        }
    }

}
